C++中定义别名的几种方式总结 | 您所在的位置:网站首页 › python 变量别名 › C++中定义别名的几种方式总结 |
背景
在代码编辑过程中,为了书写省事或者更容易理解,通常会自定义别名,包括类型别名、方法别名等。在 C++ 中定义别名有以下几种方式。 #define①.概述 #define 是宏定义,作用就是将一个标识符定义为一个字符串,源程序中所有的该标识符均以指定的字符串代替,在预编译阶段执行。 ②.定义类型别名 #include "iostream" using namespace std; #define intPtr int * //定义一个 int * 指针类型 int main() { intPtr x = new int(6); cout PRINTHELLO(); system("pause"); return 0; }运行结果如下: ①.概述 typedef是用来申请类型别名的,本质是为数据类型起了一个别名,也相当于定义了一个新的类型。 ②.定义数据类型 #include "iostream" using namespace std; typedef int * IntPtr; int main() { IntPtr x = new int(6); cout return a + b; } int sub(int a, int b) { return a - b; } int main() { func * calFunc; calFunc = add; cout return a + b; }⑥.定义函数指针 #include "iostream" using namespace std; typedef int (*func)(int a,int b); int add(int a, int b) { return a + b; } int main() { func calFunc = add; cout typedef map type; }; strMap::type m_map; using①.概述 c++11中通过 using来定义别名,比typedef更容易阅读 ②.定义类型别名 using intPtr = int *; intPtr x = new int(6);③.定义函数指针别名 #include "iostream" using namespace std; using func = int(*)(int a, int b); int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int main() { func calFunc = add; cout return a - b; } int main() { func *calFunc = add; cout |
CopyRight 2018-2019 实验室设备网 版权所有 |